﻿package com.myp.soundtrack
{
    import com.greensock.*;
    import com.greensock.plugins.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;

    public class Soundtrack extends Object
    {
        private var _started:Boolean;
        private var channel:SoundChannel;
        private var _paused:Boolean;
        private var sound:Sound;
        private var _file:String;
        private static const FADE_TIME:Number = 1;
        private static var instance:Soundtrack;
        public static var verbose:Boolean = true;
        private static const MAX_VOLUME:Number = 1;

        public function Soundtrack()
        {
            TweenPlugin.activate([VolumePlugin]);
            return;
        }// end function

        public function start() : void
        {
            if (this._started)
            {
                this.say("Ya estaba inicializado!");
                return;
            }// end if
            if (!this._file)
            {
                this.say("No se ha especificado un archivo!");
                return;
            }// end if
            this.say("Cargando " + this._file);
            this.sound = new Sound();
            this.sound.load(new URLRequest(this._file));
            this.startSound();
            this.say("Token");
            this._paused = false;
            return;
        }// end function

        private function say(param1:String) : void
        {
            if (verbose)
            {
                trace("<< SOUNDTRACK : " + param1 + " >>");
            }// end if
            return;
        }// end function

        public function play() : void
        {
            this.say("Play");
            TweenLite.to(this.channel, FADE_TIME, {volume:MAX_VOLUME});
            this._paused = false;
            return;
        }// end function

        public function get paused() : Boolean
        {
            return this._paused;
        }// end function

        public function onSoundComplete(param1:Event) : void
        {
            this.startSound();
            this.say("Empezando sonido");
            return;
        }// end function

        public function togglePause() : void
        {
            if (this._paused)
            {
                this.play();
            }
            else
            {
                this.pause();
            }// end else if
            return;
        }// end function

        private function startSound() : void
        {
            this.channel = this.sound.play(0, 0, new SoundTransform(this._paused ? (0) : (MAX_VOLUME), 0));
            this.channel.addEventListener(Event.SOUND_COMPLETE, this.onSoundComplete, false, 0, true);
            return;
        }// end function

        public function set file(param1:String) : void
        {
            this._file = param1;
            return;
        }// end function

        public function pause() : void
        {
            this.say("Pausa");
            TweenLite.to(this.channel, FADE_TIME, {volume:0});
            this._paused = true;
            return;
        }// end function

        public static function getInstance() : Soundtrack
        {
            instance = instance ? (instance) : (new Soundtrack);
            return instance;
        }// end function

    }
}
